package com.giser.testapp.lib.Location;

import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.support.v4.content.ContextCompat;

import com.google.gson.Gson;
import com.giser.testapp.lib.App.EnvApplication;
import com.giser.testapp.lib.App.GlobClass;
import com.giser.testapp.lib.Tool.AlertUtil;
import com.giser.testapp.lib.Tool.EnvLog;
import com.giser.testapp.lib.Tool.PhoneStateUtil;
import com.giser.testapp.MainActivity;

/**
 * Created by Lss on 2017/11/29.
 */

public class LocationInfo {
    private LocationManager locationManager = null;
    private AndroidLocation androidLocation=null;
    private Context mContext=null;

    private long locationTime_NetWork=0;
    private float locationDistance_NetWork=0;
    private long locationTime_GPS=5000;
    private float locationDistance_GPS=0;

    public LocationInfo(Context context){
        mContext=context;
        locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        androidLocation=new AndroidLocation();
    }

    public boolean start() {
       boolean tag=false;
        EnvLog.i("start");
        switch (EnvApplication.getApplication().locationModuleTag){
            case GlobClass.LocationModule_GPSNetWork_CODE:
                if(PhoneStateUtil.isGpsProviderEnabled(mContext) && PhoneStateUtil.isNetworkConnected(mContext)){
                    tag=true;
                }
                break;
            case GlobClass.LocationModule_GPS_CODE:
                if(PhoneStateUtil.isGpsProviderEnabled(mContext)){
                    if(PhoneStateUtil.isNetworkConnected(mContext)){
                        tag=false;
                    }else{
                        tag=true;
                    }
                }
                break;
            case GlobClass.LocationModule_NetWork_CODE:
                if(PhoneStateUtil.isNetworkConnected(mContext)){
                    if(PhoneStateUtil.isGpsProviderEnabled(mContext)){
                        tag=false;
                    }else{
                        tag=true;
                    }
                }
                break;
            default:
                break;
        }
        if(tag){
            return true;
        }
EnvLog.i("123123324");
        EnvApplication.getApplication().locationModuleTag=0;
        //此处的判定是主要问题，API23之后需要先判断之后才能调用locationManager中的方法包括这里的getLastKnewnLocation方法和requestLocationUpdates方法
        if(ContextCompat.checkSelfPermission(mContext,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
            if(locationManager!=null && androidLocation!=null){
                if(PhoneStateUtil.isGpsProviderEnabled(mContext)){
                    EnvLog.i("GpsProvider");
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, locationTime_GPS, locationDistance_GPS, androidLocation);
                    EnvApplication.getApplication().locationModuleTag= GlobClass.LocationModule_GPS_CODE;
                }

                if(PhoneStateUtil.isNetworkConnected(mContext)){
                    EnvLog.i("NetWorkProvider");
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, locationTime_NetWork, locationDistance_NetWork, androidLocation);

                    if(EnvApplication.getApplication().locationModuleTag== GlobClass.LocationModule_GPS_CODE){
                        EnvApplication.getApplication().locationModuleTag= GlobClass.LocationModule_GPSNetWork_CODE;
                    }else{
                        EnvApplication.getApplication().locationModuleTag= GlobClass.LocationModule_NetWork_CODE;
                    }
                }

                if(EnvApplication.getApplication().locationModuleTag==0){
                    EnvApplication.getApplication().geographicLocation=null;
                    EnvApplication.getApplication().lastLocation=null;
                    EnvLog.i("start....");
                    AlertUtil.createDialog_GPS(mContext,"友情提示","定位服务未开启，请先开启定位服务！",0);
                    return false;
                }
            }
        }else{
            EnvLog.i("dfewsw");
            AlertUtil.createDialog(mContext,"友情提示","系统未授权定位服务！");
        }
        return true;
    }

    public GeographicLocation getLocation(){
        if(EnvApplication.getApplication().lastLocation==null){
            EnvLog.i("lastLocation is null");
            if(ContextCompat.checkSelfPermission(mContext,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
                if(PhoneStateUtil.isGpsProviderEnabled(mContext)){
                    EnvLog.i("lastLocation is null & gps");
                    EnvApplication.getApplication().lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    EnvLog.i("gps location="+EnvApplication.getApplication().lastLocation);
                }
                if(EnvApplication.getApplication().lastLocation==null){
                    if(PhoneStateUtil.isNetworkConnected(mContext)){
                        EnvLog.i("lastLocation is null & network");
                        EnvApplication.getApplication().lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        EnvLog.i("network loc="+EnvApplication.getApplication().lastLocation);
                    }
                }

                if(EnvApplication.getApplication().lastLocation!=null){
                    EnvLog.i("update lastLocation");
                    EnvApplication.getApplication().geographicLocation=LocationConverter.toGeographicLocation(mContext,EnvApplication.getApplication().lastLocation,null);
                }
            }
        }
        return EnvApplication.getApplication().geographicLocation;
    }

    public void stop() {
        EnvLog.i("stopLocationInfo");
        if(ContextCompat.checkSelfPermission(mContext,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
            if(locationManager!=null && androidLocation!=null){
                locationManager.removeUpdates(androidLocation);
            }
        }
        EnvApplication.getApplication().locationModuleTag=0;
    }

    public void getLocationInfo(){
        if(start()){
            GeographicLocation location= getLocation();
            if(location!=null){
                BaseLocation baseLocation=new BaseLocation(location.getLongitude(),location.getLatitude());
                Gson gson=new Gson();
                String mess=gson.toJson(baseLocation);
                EnvLog.i("location mess="+mess);

                ((MainActivity)mContext).responseLocInfo(mess);

                // ((MainActivity)mContext).webView.loadUrl("javascript:setLoc('"+mess+"')");
            }else{
                EnvLog.i("location=null");
                if(EnvApplication.getApplication().locationModuleTag==0){
                    EnvLog.i("locationModuleTag==0");
                    AlertUtil.createDialog_GPS(mContext,"友情提示","定位服务未开启，请先开启定位服务！",0);
                }else if(EnvApplication.getApplication().locationModuleTag==GlobClass.LocationModule_GPS_CODE){
                    EnvLog.i("locationModuleTag==880");
                    AlertUtil.createDialog_GPS(mContext,"友情提示","室内GPS信号弱 建议开启GPS、网络同时定位！",1);
                }
            }
        }
    }
}
